﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;

namespace NGU
{
    public partial class Activate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.ErrorPage = "/InfoPage.aspx?action=error&type=unknown";

            if (Request["user"] == null || Request["id"] == null)
            {
                Response.Redirect("/InfoPage.aspx?action=error&type=invalid_path");
            }
            string user = Request["user"].ToString().ToLower();
            TaiKhoanBUS bus = new TaiKhoanBUS();
            TaiKhoanDTO tk = bus.layThongTinTaiKhoan(user);
            if (tk.TrangThai != 0)
            {
                Response.Redirect("/InfoPage.aspx");
            }
            string id = Request["id"].ToString();
            string hash = bus.getMD5(user + tk.Password);
            if (id == bus.getMD5(user + tk.Password))
            {
                bool res = bus.kichHoatTaiKhoan(user);
                if (res == false)
                {
                    Response.Redirect("/InfoPage.aspx?action=error&type=unknown");
                }
                tk = bus.layThongTinTaiKhoan(user);
                if (tk.TrangThai == 0)
                {
                    Response.Redirect("/InfoPage.aspx?action=error&type=unknown");
                }
                else
                    if (tk.TrangThai == 1)
                    {
                        Session["ID"] = tk.MaTaiKhoan;
                        Session["Username"] = tk.Username;
                        Session["TrangThai"] = tk.TrangThai;
                        Session.Timeout = 30;
                    }
                    else
                        Response.Redirect("/InfoPage.aspx?action=error&type=unknown");
            }
        }
    }
}